package com.doupi.partner.manager;

import com.doupi.partner.dal.model.Administrator;
import com.doupi.partner.dal.model.AdministratorHasRole;
import com.doupi.partner.dal.model.AdministratorRole;
import com.doupi.partner.manager.authenticator.util.PasswordUtils;
import com.doupi.partner.model.BusinessRelation;
import com.doupi.partner.model.PartnerRelation;
import com.doupi.partner.service.*;
import com.doupi.partner.service.dto.AdministratorDto;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 后台用户manager
 * 
 * @author Alvin Wang
 * @version 1.0.0 @ 2017-04-17
 */
@Component
public class AdministratorManager {

	private final AdministratorService administratorService;
	private final AdministratorRoleService administratorRoleService;
	private final AdministratorHasRoleService administratorHasRoleService;
	private final PartnerRelationService partnerRelationService;
	private final BusinessRelationService businessRelationService;

	@Autowired
	public AdministratorManager(AdministratorService administratorService, AdministratorRoleService administratorRoleService, AdministratorHasRoleService administratorHasRoleService,
			PartnerRelationService partnerRelationService, BusinessRelationService businessRelationService) {
		this.administratorService = administratorService;
		this.administratorRoleService = administratorRoleService;
		this.administratorHasRoleService = administratorHasRoleService;
		this.partnerRelationService = partnerRelationService;
		this.businessRelationService = businessRelationService;
	}

	/**
	 * 初始账户
	 */
	@Transactional
	public Administrator initAdministrator(String mobile, String name, String roleCode, String password) {
		// 1. 插入用户
		Administrator administrator = new Administrator();
		administrator.setMobile(mobile);
		// administrator.setPassword(StringUtils.substring(mobile, -4)); // 默认密码手机号后四位
		administrator.setPasswordSalt(PasswordUtils.generatePasswordSalt());
		administrator.setPassword(PasswordUtils.encryptPassword(password, administrator.getPasswordSalt()));
		administrator.setName(name);
		administrator.setStatus(Byte.valueOf("1"));
		administratorService.insert(administrator);

		// 2. 查询角色，并插入用户角色对应关系
		AdministratorRole administratorRole = administratorRoleService.selectAdministratorRoleByCode(roleCode);
		AdministratorHasRole administratorHasRole = new AdministratorHasRole();
		administratorHasRole.setAdministratorId(administrator.getId());
		administratorHasRole.setAdministratorRoleId(administratorRole.getId());
		administratorHasRoleService.insert(administratorHasRole);

		return administrator;
	}

	/**
	 * 根据adminId查询合伙人关系
	 */
	public void wrapAdministratorDtoWithRelation(AdministratorDto administratorDto) {
		// 1. 查询合伙人关系
		PartnerRelation partnerRelation = new PartnerRelation();
		partnerRelation.setAdministratorId(administratorDto.getId());
		List<PartnerRelation> list = partnerRelationService.queryPartnerRelations(partnerRelation, new RowBounds(0, 1));
		if (!list.isEmpty()) {
			administratorDto.setPartnerRelation(list.get(0));
			return;
		}

		// 2. 合伙人关系不存在 查询商家关系
		BusinessRelation businessRelation = new BusinessRelation();
		businessRelation.setAdministratorId(administratorDto.getId());
		List<BusinessRelation> businessRelations = businessRelationService.queryBusinessRelations(businessRelation, new RowBounds(0, 1));
		if (!businessRelations.isEmpty()) {
			administratorDto.setBusinessRelation(businessRelations.get(0));
		}
	}
}
